/*
* @Author: An
* @Date:   2017-07-16 14:01:11
* @Last Modified by:   An
* @Last Modified time: 2017-07-16 15:17:07
*/

'use strict';
require('page/common/header/index.js');
require('page/common/nav/index.js');
require('./index.css');
var _xj = require('util/xj.js');
var navSide = require('page/common/nav-side/index.js');
var _order = require('service/order-service.js');
var templateIndex = require('./index.string');
var Pagination = require('util/pagination/index.js');

// page逻辑
var page = {
	data : {
		listParam : {
			pageNum : 1,
			pageSize : 10
		}
	},
	init : function() {
		this.onLoad();
	},
	onLoad : function() {
		this.loadOrderList();
		// 初始化左侧菜单
		navSide.init({
			name : 'order-list'
		});
	},
	loadOrderList : function() {
		var _this = this,
			orderListHtml = '',
			$listCon = $('.order-list-con');
		$listCon.html('<div class="loading"></div>');
		_order.getOrderList(this.data.listParam, function(res) {
			_this.dataFilter(res);
			orderListHtml = _xj.renderHtml(templateIndex, res);
			$listCon.html(orderListHtml);
			_this.loadPagination({
				hasPreviousPage : res.hasPreviousPage,
                prePage         : res.prePage,
                hasNextPage     : res.hasNextPage,
                nextPage        : res.nextPage,
				pageNum 		: res.pageNum, 
				pages 			: res.pages
			});
		}, function(errMsg) {
			$listCon.html('<p class="err-tip">加载订单失败，请刷新后重试！</p>');
		});
	},
	// 加载分页信息
    loadPagination : function(pageInfo){
        var _this = this;
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container : $('.pagination'),
            onSelectPage : function(pageNum){
                _this.data.listParam.pageNum = pageNum;
                _this.loadOrderList();
            }
        }));
    },
	// 数据的适配
	dataFilter : function(data) {
		data.isEmpty = !data.list.length;
	}
};

$(function() {
	page.init();
});